package com.ruoyi.common.utils;

import java.util.*;
import java.util.regex.Pattern;

import static com.ruoyi.common.utils.ResumeUtil.dealJianliSpecialPartToShow;

/**
 * TYF 表单格式化工具类
 * 2022 10 29
 *
 */
public class FormUtils {


    private static final String regexStart = "\\(|\\（";

    private static final String regexEnd = "\\)|\\）";


    public static List<Map<String, String>> getA1701ListHB(String a1701) {
        if (StringUtils.isBlank(a1701)) {
            return new ArrayList<>();
        }

        String[] st = a1701.replaceAll("\n", "\r").replaceAll(regexStart, "（").replaceAll(regexEnd, "）").split("\r");
        List<Map<String, String>> listMap = new ArrayList<>();
        String[] sts = null;
        List<Integer> listInt = null;
        for (int j = 0; j < st.length; j++) {
            String str = st[j];
            Map<String, String> map = new HashMap<>();
            String string = "";
            str = str.trim().replaceAll("&nbsp;", " ").replaceAll("　　　　", " ");
            sts = str.split(" ");
            if (sts.length > 0) {
                if (j == st.length - 1 && sts.length == 1 &&
                        ResumeUtil.isHaveString(sts[0])) {
                    map.put("key", "");
                    map.put("value", sts[0]);
                    listMap.add(map);

                    break;
                }
                map.put("key", sts[0]);
                if (sts.length > 1) {
                    for (int i = 1; i < sts.length; i++) {
                        String newStr = "";
                        if (StringUtils.isNotBlank(sts[i])) {
                            listInt = new ArrayList<>();
                            listInt = ResumeUtil.getSpecialSignIndexArray(sts[i], "（", 0, listInt);
                            listInt = ResumeUtil.getSpecialSignIndexArray(sts[i], "(", 0, listInt);
                            Collections.sort(listInt);
                            newStr = ResumeUtil.dealJianliSpecialPartToShow(sts[i], listInt);
                            string = string + newStr;
                        }
                    }


                    map.put("value", string);
                } else {
                    map.put("value", string);
                }
                listMap.add(map);
            }
        }

        return listMap;
    }




    public static List<Map<String, Object>> getA1701List(String a1701) {
        return getA1701List(a1701, false);
    }

    public static List<Map<String, Object>> getA1701List(String a1701, boolean isSpecialDeal) {
        if (StringUtils.isBlank(a1701)) {
            return new ArrayList<>();
        }

        String[] st = a1701.replaceAll("\n", "\r").replaceAll(regexStart, "（").replaceAll(regexEnd, "）").split("\r");
        List<Map<String, Object>> listMap = new ArrayList<>();
        String[] sts = null;
        List<Integer> listInt = null;
        for (int j = 0; j < st.length; j++) {
            String str = st[j];
            Map<String, Object> map = new HashMap<>();
            String string = "";
            str = str.trim().replaceAll("&nbsp;", " ").replaceAll("　　　　", " ");
            sts = str.split(" ");
            if (sts.length > 0) {
                if (j == st.length - 1 && sts.length == 1) {
                    map.put("key", "");
                    if (isSpecialDeal) {
                        map.put("value", sts[0].trim());
                    } else {
                        map.put("value", sts[0]);
                    }
                    listMap.add(map);

                    break;
                }
                map.put("key", sts[0]);
                if (sts.length > 1) {
                    for (int i = 1; i < sts.length; i++) {
                        String newStr = "";
                        if (StringUtils.isNotBlank(sts[i])) {
                            listInt = new ArrayList<>();
                            listInt = getSpecialSignIndexArray(sts[i], "（", 0, listInt);
                            listInt = getSpecialSignIndexArray(sts[i], "(", 0, listInt);
                            Collections.sort(listInt);
                            newStr = dealJianliSpecialPartToShow(sts[i], listInt);
                            string = string + newStr;
                        }
                    }


                    map.put("value", string);
                } else {
                    map.put("value", string);
                }
                listMap.add(map);
            }
        }

        return listMap;
    }

    public static String getA1701String(List<Map<String, Object>> listMap) {
        String A1701 = "";
        for (Map<String, Object> map : listMap) {
            String key = (String)map.get("key");
            String value = (String)map.get("value");
            if (StringUtils.isNotBlank(A1701)) {
                A1701 = A1701 + '\n' + key + "&nbsp;&nbsp;" + value; continue;
            }
            A1701 = key + "&nbsp;&nbsp;" + value;
        }

        return A1701;
    }

    public static List<Integer> getSpecialSignIndexArray(String str, String sign, int fromIndex, List<Integer> list) {
        int index = str.indexOf(sign, fromIndex);
        if (index != -1) {
            list.add(Integer.valueOf(index));
            return getSpecialSignIndexArray(str, sign, index + 1, list);
        }
        return list;
    }



    public static String getJianliFromListMap(List<Map<String, Object>> listmap) {
        StringBuffer sb = new StringBuffer();
        for (Map<String, Object> lom : listmap) {
            String A1905 = (lom.get("start_time") == null) ? "" : String.valueOf(lom.get("start_time"));
            A1905 = StringUtils.getTopSix(A1905);
            String A1910 = (lom.get("end_time") == null) ? "" : String.valueOf(lom.get("end_time"));
            A1910 = StringUtils.getTopSix(A1910);
            String A1920 = (lom.get("duities") == null) ? "" : String.valueOf(lom.get("duities"));
            if (StringUtils.isBlank(A1910)) {
                if (StringUtils.isNotBlank(A1905)) {
                    sb.append(addSpot(A1905) + "--         ");
                } else {
                    sb.append("\r                  ");
                }
            } else {
                sb.append(addSpot(A1905) + "--" + addSpot(A1910) + "  ");
            }
            if (StringUtils.isNotBlank(A1920)) {
                A1920 = A1920.replaceAll("\"", "“");
            }
            List<Integer> list = new ArrayList<>();
            list = getSpecialSignIndexArray(A1920, "（", 0, list);
            list = getSpecialSignIndexArray(A1920, "(", 0, list);
            Collections.sort(list);
            String a1920new = dealJianliSpecialPartToDB(A1920, list);
            sb.append(a1920new + "\r");
        }
        return sb.toString();
    }

    public static String addSpot(String str) {
        if (StringUtils.isBlank(str)) {
            return "";
        }
        String begin = str.substring(0, 4);
        String last = str.substring(4);
        return begin + "." + last;
    }


    public static String dealJianliSpecialPartToDB(String str, List<Integer> list) {
        Pattern pattern = Pattern.compile("\\（\\d{4}\\.\\d{1,2}--\\d{4}\\.\\d{1,2}");
        Pattern pattern1 = Pattern.compile("\\(\\d{4}\\.\\d{1,2}--\\d{4}\\.\\d{1,2}");
        Pattern pattern2 = Pattern.compile("\\（\\d{4}\\.\\d{1,2}--[至][今]");
        Pattern pattern3 = Pattern.compile("\\(\\d{4}\\.\\d{1,2}--[至][今]");

        StringBuffer sb = new StringBuffer(str);
        for (int i = list.size() - 1; i >= 0; i--) {
            int in = ((Integer)list.get(i)).intValue();
            if (str.substring(in).length() >= 17)
            {
                if (pattern.matcher(str.substring(in, in + 17) + "").matches() || pattern1.matcher(str.substring(in, in + 17) + "").matches()) {
                    sb.insert(in, "\r\n                  ");
                }
            }
            if (str.substring(in).length() >= 12 && (
                    pattern2.matcher(str.substring(in, in + 12) + "").matches() || pattern3.matcher(str.substring(in, in + 12) + "").matches())) {
                sb.insert(in, "\r\n                  ");
            }
        }


        return sb.toString();
    }
}
